package icu.fancy.common;

import java.io.Serializable;

/**
 * <p>
 * 响应类
 * </p>
 *
 * @author Alan
 */
public class R<T> implements Serializable {

    private static final long serialVersionUID = -3544516772996462997L;

    /**
     * 成功
     */
    public static final int SUCCESS = 200;
    /**
     * 信息
     */
    public static final int INFO = 300;
    /**
     * 失败
     */
    public static final int FAILURE = 400;
    /**
     * 响应码
     */
    private Integer code;

    /**
     * 响应提示信息
     */
    private String message;

    /**
     * 成功或者失败
     */
    private Boolean success;

    /**
     * 传输的数据
     */
    private T data;

    private R() {
    }

    private R(Boolean success, T data, int code, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.success = success;
    }

    /**
     * 成功
     */
    public static <T> R<T> success() {
        return success(null);
    }

    public static <T> R<T> success(T data) {
        return success(data, "");
    }

    public static <T> R<T> success(T data, String message) {
        return new R<T>(true, data, SUCCESS, message);
    }

    /**
     * 信息
     */

    public static <T> R<T> info(Boolean success) {
        return new R<T>(success, null, INFO, success ? "success~~" : "failure~~");
    }

    public static <T> R<T> info(Boolean success, String message) {
        return new R<T>(success, null, INFO, message);
    }

    public static <T> R<T> info(Boolean success, T data, String message) {
        return new R<T>(success, data, INFO, message);
    }

    /**
     * 失败
     */
    public static <T> R<T> failure() {
        return failure("操作失败");
    }
    public static <T> R<T> failure(String message) {
        return failure(null, message);
    }
    public static <T> R<T> failure(T data, String message) {
        return new R<T>(false, data, FAILURE , message);
    }


    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", success=" + success +
                ", data=" + data +
                '}';
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
